﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionAutoEcoleModel;
using log4net;
using GestionAutoEcole.DAO.Util;
using System.Data.Objects;


namespace GestionAutoEcole.DAO.Impl
{
    public class DocumentDaoImpl : BaseRepository<document>
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DocumentDaoImpl));

        private static volatile DocumentDaoImpl instance;
        private static object syncRoot = new Object();

        private DocumentDaoImpl() { }

        public static DocumentDaoImpl getInstance()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new DocumentDaoImpl();
                }
            }

            return instance;
        }
        //création d'un groupe
        public override void Create(document doc)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Create");
                }
                using (var context = new autoecolebase())
                {
                    // il faut plutot utiliser ça : context.VUGroupe.AddObject(grp);
                    context.document.AddObject(doc);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Create : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Create");
                }
            }
        }

        //modification
        public override void Update(document entity)
        {

        }


        //Supprimer tous les groupe
        public override void DeleteAll()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin DeleteAll");
                }
                using (var context = new autoecolebase())
                {
                    var documentList = context.document;
                    foreach (var doc in documentList)
                    {
                        context.DeleteObject(doc);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteAll : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End DeleteAll");
                }
            }
        }

        //Supprimer un groupe
        public override void Delete(document doc)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Delete");
                }
                using (var context = new autoecolebase())
                {
                    var pers = context.document.Where(u => u.DOCUMENT_ID == doc.DOCUMENT_ID).First();
                    context.DeleteObject(pers);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Delete");
                }
            }
        }

        //Récupérer tous les groupes
        public override List<document> GetList()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin GetList");
                }
                using (var context = new autoecolebase())
                {
                    return context.document.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("GetList : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End GetList");
                }
            }
        }

        //Récupérer un groupe avec son Id
        public override document FindById(int id)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindById");
                }
                using (var context = new autoecolebase())
                {
                    var grp = context.document.Where(u => u.DOCUMENT_ID == id);
                    return grp.First();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindById : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindById");
                }
            }
        }

        //Récupérer une liste d'groupes avec un critère de recherche
        public override List<document> FindByCriteria(Object criteria)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindByCriteria");
                }
                using (var context = new autoecolebase())
                {

                    StringBuilder queryString =
                        new StringBuilder(@"SELECT VALUE grp FROM autoecolebase.document as grp");
                    SelectBuilder sb = new SelectBuilder();

                    document grp = (document)criteria;
                    if (grp.DOCUMENT_LIBELLE != null && !grp.DOCUMENT_LIBELLE.Equals(""))
                    {
                        sb.AndSearchLike("grp.DOCUMENT_LIBELLE", grp.DOCUMENT_LIBELLE.ToString());
                    }

                    if (grp.DOCUMENT_COMMENTAIRE != null && !grp.DOCUMENT_COMMENTAIRE.Equals(""))
                    {
                        sb.AndSearchLike("grp.DOCUMENT_COMMENTAIRE", grp.DOCUMENT_COMMENTAIRE.ToString());
                    }

                    queryString.Append(sb.getQueryString());
                    ObjectQuery<document> query = new ObjectQuery<document>(queryString.ToString(), context);
                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindByCriteria : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindByCriteria");
                }
            }
        }

    }
}
